package com.sam.android.callersl;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;



public class CallAndSMSListener extends BroadcastReceiver {
	  

	@Override
	public void onReceive(Context context, Intent intent) {
	  
		Log.d("CallerSL"," onReceive: ");

	  String action = intent.getAction();
	 
	         if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
	          if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
	                              TelephonyManager.EXTRA_STATE_RINGING)) {
	        	 
	        	
	        	  
		            
		            	IncomingCallActivity.launch(context, intent);
		            	
		            
		             
	        	// Toast.makeText(context, intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER), Toast.LENGTH_LONG).show();
	        	
	             doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
	          }
	          
	         
	          if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                      TelephonyManager.EXTRA_STATE_OFFHOOK)) {
	        	 
	        	  CallerSLActivity mainactInst =  CallerSLActivity.instance();
	        	  IncomingCallActivity incallactInst =  IncomingCallActivity.instance();
	        	  if(mainactInst != null)  { // your activity can be seen, and you can update it's context 
	        		  mainactInst.sendMyLocation();
	        	      
	        	   }
	        	  if(incallactInst != null)
	        	  {
	        		  
	        		  Log.d("CallerSL","Call answered..");
	        	       incallactInst.onCallAnswered();
	        	  }

	          }
	         }
	        
	         if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                     TelephonyManager.EXTRA_STATE_IDLE)) {
	        	 
	        	 IncomingCallActivity incallactInst =  IncomingCallActivity.instance();
	        	 if(incallactInst != null)
	        	  {
	        		  
	        		  Log.d("CallerSL","Phone Idle so end hide callerSL..");
	        	       incallactInst.onCallAnswered();
	        	  }
	         }
	        	 
	       
	         
	  
	 }
	 
	 private void doSomething(String number){
	  Log.d("CallerSL",number);
	 
	 }

	}